package com.dlwx.firefighting.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.dlwx.firefighting.R;
import com.dlwx.firefighting.bean.OrderDetailBean;
import com.dlwx.firefighting.config.Constant;
import com.dlwx.firefighting.utils.DialogUtils;
import com.dlwx.firefighting.utils.SPUtils;
import com.dlwx.firefighting.utils.ToastUtils;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.iarcuschin.simpleratingbar.SimpleRatingBar;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class CommentGoodsActivity extends BaseActivity {

    @BindView(R.id.title_left_one_btn)
    ImageView titleLeftOneBtn;
    @BindView(R.id.tv_middle_title)
    TextView tvMiddleTitle;
    @BindView(R.id.recycler)
    LRecyclerView recycler;
    @BindView(R.id.image)
    ImageView image;
    @BindView(R.id.rating_bar)
    SimpleRatingBar ratingBar;
    @BindView(R.id.content_et)
    EditText contentEt;
    @BindView(R.id.send_btn)
    Button sendBtn;
    OrderDetailBean.XiangBean bean;
    private String userId;
    private int grade=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_comment_goods);
        ButterKnife.bind(this);
        bean= (OrderDetailBean.XiangBean) getIntent().getSerializableExtra("bean");
        userId = (String) SPUtils.get(this, Constant.USER_ID, "");
        initView();
    }

    private void initView() {
        Glide.with(getApplicationContext()).load(bean.getGoods_thumb()).error(R.mipmap.error_small).into(image);
        titleLeftOneBtn.setVisibility(View.VISIBLE);
        tvMiddleTitle.setVisibility(View.VISIBLE);
        tvMiddleTitle.setText("发评论");
        ratingBar.setOnRatingBarChangeListener(new SimpleRatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(SimpleRatingBar simpleRatingBar, float rating, boolean fromUser) {
                grade=Math.round(rating);
            }
        });

    }

    @OnClick({R.id.title_left_one_btn, R.id.send_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_left_one_btn:
                CommentGoodsActivity.this.finish();
                break;
            case R.id.send_btn:
                String content = contentEt.getText().toString();
                if (TextUtils.isEmpty(content)) {
                    ToastUtils.toastForShort(CommentGoodsActivity.this, "内容不能为空！");
                    return;
                }
                publishComment(content);
                break;
        }
    }

    private void publishComment(String content) {
        DialogUtils.showProgressDlg(this,"");
//      comment_type  0、商品 1、文章2、视频
        OkGo.<String>post(Constant.CONFIRM_COMMENT)
                .tag(this)
                .params("user_id", userId)
                .params("content", content)
                .params("comment_type", 0)
                .params("id_value", bean.getGoods_id())
                .params("comment_rank", grade)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        DialogUtils.stopProgressDlg();
//                        String res = response.body().toString();
//                        if (TextUtils.isEmpty(res)) {
//                            ToastUtils.toastForShort(CommentGoodsActivity.this, "评论失败！");
//                        } else {
//                            BaseBean bean = new Gson().fromJson(res, BaseBean.class);
//                            if (bean.getStatus() == Constant.SUCESS) {
//                                CommentGoodsActivity.this.finish();
//                            }
//                            ToastUtils.toastForShort(CommentGoodsActivity.this, bean.getMsg());
//                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        DialogUtils.stopProgressDlg();
                        ToastUtils.toastForShort(CommentGoodsActivity.this, getString(R.string.bad_net));
                        super.onError(response);
                    }
                });
    }
}
